package com.reliable.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.reliable.api.dto.user.ReliableUserLite;
import com.reliable.common.utils.DateUtils;
import com.reliable.framework.utils.JwtUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;


/**
 * MybatisPlus自动填充
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Long userBy = null;
        try {
            ReliableUserLite currentUser = JwtUtils.getCurrentUser();
            Long id = currentUser.getId();
            userBy = id;
        } catch (Exception e) {
        }
        this.setFieldValByName("createTime", DateUtils.getNowDate(), metaObject);
        this.setFieldValByName("createBy", userBy, metaObject);
        this.setFieldValByName("updateTime", DateUtils.getNowDate(), metaObject);
        this.setFieldValByName("updateBy", userBy, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long userBy = null;
        try {
            ReliableUserLite currentUser = JwtUtils.getCurrentUser();
            Long id = currentUser.getId();
            userBy = id;
        } catch (Exception e) {
        }
        this.setFieldValByName("updateTime", DateUtils.getNowDate(), metaObject);
        this.setFieldValByName("updateBy", userBy, metaObject);
    }
}
